﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Configuration;
using System.IO;

namespace TestForSomee.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private string path = null;

        public ActionResult Index()
        {
            path = Server.MapPath("~/dataBase.txt");
            string strCount = GetCountVisits();
            int count = -1;
            if (!string.IsNullOrWhiteSpace(strCount) && Int32.TryParse(strCount, out count))
            {
                SetCountVisits(++count);
                ViewData["CountVisits"] = count;
                int i = 99127;
                var a = i;
            }
            else
            {
                ViewData["CountVisits"] = "HZ";
            }

            ViewData["Message"] = "Тестовий запуск!!!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        private string GetCountVisits()
        {
            string count = null;
            try
            {
                TextReader tr = new StreamReader(path);
                // write a line of text to the file
                count = tr.ReadLine();
                // close the stream
                tr.Close();
            }
            catch (Exception) { }

            return count;
        }

        private void SetCountVisits(int count)
        {
            try
            {
                TextWriter tw = new StreamWriter(path);
                // write a line of text to the file
                tw.WriteLine(count);
                // close the stream
                tw.Close();
            }
            catch (Exception) { }
        }
    }
}
